using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace TrovaIntruso.core
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class Menu : Screen
    {
        Button newgame,resume,help,ringraziamenti,opzioni,medium,easy,hard;
        Popup ringrpop,option,aiuto,difficulty;
        Texture2D background;
        TextView tx;

        public bool ContinueEnabled { get; set; }
        public Menu(Game game)
            : base(game)
        {
            // TODO: Construct any child components here
             tx= new TextView(Game);
            initButton();
            background = Game.Content.Load<Texture2D>("backgroundmenu");
           
        }

        public void initButton(){

            //ADD BUTTON CODE
            List<Rectangle> rectlist = StaticOperation.GetScreenDivision(4, new Vector2(256, 64), new Vector2(0, 116));
            int current = 0;
            newgame = new Button(Game, new Sprite((Texture2D)StaticOperation.StaticResources["newgame"], rectlist[current++]));
            AddComponent(newgame);
            if (ContinueEnabled)
            {
                resume = new Button(Game, new Sprite((Texture2D)StaticOperation.StaticResources["continue"], rectlist[current++]));
                AddComponent(resume);
                resume.BindClick(new Button.ClickDelegate(resume_click));
            }
            help = new Button(Game, new Sprite((Texture2D)StaticOperation.StaticResources["help"], rectlist[current++]));
            AddComponent(help);
            rectlist[3]=new Rectangle(rectlist[3].X,rectlist[3].Y,330,rectlist[3].Height);
            opzioni = new Button(Game, new Sprite((Texture2D)StaticOperation.StaticResources["opzioni"], rectlist[current]));
            AddComponent(opzioni);
            
            //BIND CLICK CODE
            newgame.BindClick(new Button.ClickDelegate(newgameClick));
            opzioni.BindClick(new Button.ClickDelegate(OptClick));
            help.BindClick(new Button.ClickDelegate(HelpClick));
            //POPUP CODE
            InitPopup();
        }


        private void InitPopup() {
            InitOptions();
            InitDifficulty();
            //Popup ringraziamenti
            ringrpop = new Popup(Game);
            ringrpop.Toggle(false);
            AddComponent(ringrpop);
            ringrpop.AddComponent(tx);
            aiuto = new Popup(Game);
            aiuto.Toggle(false);
            AddComponent(aiuto);
            aiuto.AddComponent(tx);
            
        }

        private void InitDifficulty()
        {
            difficulty = new Popup(Game);
            AddComponent(difficulty);
            int x=(difficulty.surface.X+difficulty.surface.Width)/2-128;
            int gap=difficulty.surface.Height/3,
                y1=(difficulty.surface.Y+gap)/2-32;

            easy = new Button(Game, new Sprite((Texture2D)StaticOperation.StaticResources["facile"], new Rectangle(x, y1, 256, 64)));
            y1 += gap;
            medium = new Button(Game, new Sprite((Texture2D)StaticOperation.StaticResources["medio"], new Rectangle(x, y1,256, 64)));
            y1 += gap;
            hard = new Button(Game, new Sprite((Texture2D)StaticOperation.StaticResources["difficile"], new Rectangle(x, y1, 256, 64)));
            difficulty.AddComponent(easy);
            difficulty.AddComponent(medium);
            difficulty.AddComponent(hard);

            easy.BindClick(new Button.ClickDelegate(easy_click));
            medium.BindClick(new Button.ClickDelegate(medium_click));
            hard.BindClick(new Button.ClickDelegate(hard_click));
            difficulty.Toggle(false);
        }

        private void easy_click() {
            difficulty.Toggle(false);
            (Game as Game1).NewGame(7);
        }
        private void medium_click() { 
            difficulty.Toggle(false);
            (Game as Game1).NewGame(5);
        }
        private void hard_click() { 
            difficulty.Toggle(false);
            (Game as Game1).NewGame(3);
        }
        Button vol, mute;
        bool guardia = false;
        private void InitOptions() {
            //POPUP OPZIONI
            
            option = new Popup(Game);
            vol=new Button(Game,new Sprite(StaticOperation.Icons["volume"],new Rectangle(option.surface.Width/2-44,200,128,128)));
            mute=new Button(Game,new Sprite(StaticOperation.Icons["mute"],new Rectangle(option.surface.Width/2-44,200,128,128)));
            if (StaticOperation.AudioEnabled)
                mut_click();
            else vol_click();
            vol.BindClick(new Button.ClickDelegate(vol_click));
            mute.BindClick(new Button.ClickDelegate(mut_click));
            option.Toggle(false);
            option.AddComponent(vol);
            option.AddComponent(mute);
            int margin=option.surface.Width/2-128;
            AddComponent(option);
            ringraziamenti=new Button(Game,new Sprite((Texture2D)StaticOperation.StaticResources["ringraziamenti"],new Rectangle(margin,60,256,64)));
            ringraziamenti.BindClick(new Button.ClickDelegate(RingrClick));
            option.AddComponent(ringraziamenti);
        }

        private void vol_click() {
            if (!guardia)
            {
                StaticOperation.AudioEnabled = false;
                vol.Visible = false;
                vol.Enabled = false;
                mute.Visible = true;
                mute.Enabled = true;
                guardia = true;
            }
            
        }


        private void mut_click() {
            if (!guardia)
            {
                StaticOperation.AudioEnabled = true;
                vol.Visible = true;
                vol.Enabled = true;
                mute.Visible = false;
                mute.Enabled = false;
                guardia = true;
            }
        }

        protected override void LoadContent()
        {
            base.LoadContent();
        }

        public void newgameClick() {
            difficulty.Toggle(true);
        }

        private void OptClick() {
            option.Toggle(true);
        }

        private void HelpClick() {
            tx.surface = new Rectangle(ringrpop.surface.X + 20, ringrpop.surface.Y, ringrpop.surface.Width - 40, ringrpop.surface.Height - 50);
            tx.StartPosition = (ringrpop.surface.Y + ringrpop.surface.Height) / 4;
            tx.Text = StaticOperation.RingrText;
            tx.CanScroll = true;
            tx.AutoScroll = false;
            aiuto.Toggle(true);
        }

        public void RingrClick() {
            tx.surface = new Rectangle(ringrpop.surface.X + 20, ringrpop.surface.Y, ringrpop.surface.Width - 40, ringrpop.surface.Height - 50);
            tx.StartPosition = (ringrpop.surface.Y + ringrpop.surface.Height) / 4;
            tx.Text = StaticOperation.RingrText;
            tx.AutoScroll = true;
            tx.CanScroll = false;
            ringrpop.Toggle(true);
            option.Toggle(false);

        }

        private void resume_click() {
            (Game as Game1).Resume();
        }

        protected override void SelfUpdate(GameTime gt)
        {
            if (newgame != null &&  opzioni != null && help != null)
            {
                if (ringrpop.Visible || aiuto.Visible || option.Visible || difficulty.Visible)
                {
                    newgame.Enabled = false;
                    if(ContinueEnabled) resume.Enabled = false;
                    opzioni.Enabled = false;
                    help.Enabled = false;
                }
                else
                {
                    newgame.Enabled = true;
                    if (ContinueEnabled) resume.Enabled = true;
                    opzioni.Enabled = true;
                    help.Enabled = true;
                }
            }
            base.SelfUpdate(gt);
        }

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
            guardia = false;
        }

        public override void Draw(GameTime gameTime)
        {
            SpriteBatch s = StaticOperation.Batch;
            s.Begin();
            s.Draw(background, new Rectangle(0, 0, 480, 800), Color.White);
            s.End();
            base.Draw(gameTime);
        }

        internal void Removebuttons()
        {
            RemoveComponent(newgame);
            RemoveComponent(resume);
            RemoveComponent(opzioni);
            RemoveComponent(help);
        }
    }
}
